﻿using _2112A_2_MES._chubowen;
using _2112A_2_MES._wangweihao;
using _2112A_2_MES._XiangHe;
using _2112A_2_MES.SuJiaHaos;
using _2112A_2_MES.SuJiaHaos.ProductionPiickings;
using _2112A_2_MES.SuJiaHaos.ProductionPiickings;
using _2112A_2_MES.SuJiaHaos.WareHouse;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Test.WangWeiHao;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace _2112A_2_MES.SuJiaHao
{
    /// <summary>
    /// 生产领料服务实现
    /// </summary>
    [ApiExplorerSettings(GroupName = "苏佳豪")]
    public class ProductionPiickingService : ApplicationService, IProductionPiickingService
    {
        /// <summary>
        /// 学生仓储
        /// </summary>
        private readonly IRepository<ProductionPiicking, int> _productionRepository;
        /// <summary>
        /// 狗仔方法
        /// </summary>
        /// <param name="productionRepository">学生仓储</param>
        public ProductionPiickingService(IRepository<ProductionPiicking, int> productionRepository)
        {
            _productionRepository = productionRepository;
        }

        /// <summary>
        /// 获取生产领料列表
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页容量</param>
        /// <returns></returns>
        public async Task<ReturnSelect<object>> GetProductionPiickingList(int pageSize, int pageIndex)
        {
            // 获取生产领料列表
            var productionPiickingList = await _productionRepository.GetListAsync();

            //// 使用 WhereIf 进行条件筛选
            //var wareHouseLists = wareHouseList
            //    .WhereIf(!string.IsNullOrEmpty(warehouseCode), x => x.WarehouseCode.Contains(warehouseCode))
            //    .WhereIf(!string.IsNullOrEmpty(warehouseName), x => x.WarehouseName.Contains(warehouseName))
            //    .ToList();

            // 计算总记录数和当前页记录
            var totalCount = productionPiickingList.Count;
            var pageList = productionPiickingList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var result = AutoMapperExt.MapToList<ProductionPiicking>(pageList);

            //返回结果
            var data = new { TotalCount = totalCount, PageList = result };
            return new ReturnSelect<object>(true, "0", result != null && result.Count > 0 ? data : null);

        }

        /// <summary>
        /// 生产领料添加操作
        /// </summary>
        /// <param name="input">生产领料实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> GetCreateProductionPiicking(CreateProductionPiickingDTO input)
        {
            var productionPiickings = ObjectMapper.Map<CreateProductionPiickingDTO, ProductionPiicking>(input);
            productionPiickings.CreateBy = "崔二强";
            productionPiickings.CreateTime = DateTime.Now;
            await _productionRepository.InsertAsync(productionPiickings);
            return productionPiickings != null ? 1 : 0;
        }

        /// <summary>
        /// 生产领料删除
        /// </summary>
        /// <param name="id">条件编号</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> DeleteProductionPiicking(int id)
        {
            var productionPiickingDel = await _productionRepository.GetAsync(id);
            await _productionRepository.DeleteAsync(productionPiickingDel);
            return productionPiickingDel != null ? 1 : 0;
        }
        /// <summary>
        /// 生产领料反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ProductionPiicking> GetProductionPiicking(int id)
        {
            var productionPiicking = await _productionRepository.FirstOrDefaultAsync(x => x.Id == id);
            return productionPiicking;
        }

        /// <summary>
        /// 生产领料信息修改
        /// </summary>
        /// <param name="input">生产领料实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdateProductionPiicking(CreateProductionPiickingDTO input)
        {
            var productionPiicking = ObjectMapper.Map<CreateProductionPiickingDTO, ProductionPiicking>(input);
            productionPiicking.UpdateBy = "崔大强";
            productionPiicking.UpdateTime = DateTime.Now;
            await _productionRepository.UpdateAsync(productionPiicking);
            return productionPiicking != null ? 1 : 0;
        }

    }
}
